package com.douwong.student.activity;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.model.CourseTab;
import com.douwong.student.model.StatusCode;
import com.douwong.student.rxjava.NetWork;
import com.douwong.student.utils.DateTools;
import com.douwong.student.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * @autor yichao
 * @time 2017/3/2814:43
 * @des 课程表
 */
public class CourseTabActivity extends BaseActivity {
    private TextView right_tv,morning,afternoon,currentDay;
    private ImageView mMImageViewOne,mImageViewTwo,mImageViewThreed,mImageViewFour,mImageViewFive;
    private GridView course;
    private String [] mWeekOfDays;
    private String[][] courseArr = new String[7][6];
    private ArrayList<String> strList = new ArrayList<String>();
    private ClasstableAdapter amAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.act_coursetab);
        right_tv = (TextView) findViewById(R.id.save_text);
        morning = (TextView) findViewById(R.id.morning);
        mMImageViewOne = (ImageView) findViewById(R.id.image_weekdau_one);
        mImageViewTwo = (ImageView) findViewById(R.id.image_weekdau_two);
        mImageViewThreed = (ImageView) findViewById(R.id.image_weekdau_threed);
        mImageViewFour = (ImageView) findViewById(R.id.image_weekdau_four);
        mImageViewFive = (ImageView) findViewById(R.id.image_weekdau_five);
        course = (GridView) findViewById(R.id.course);
        afternoon = (TextView) findViewById(R.id.afternoon);
        currentDay = (TextView) findViewById(R.id.currrent_day);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        findViewById(R.id.go_back_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        morning.setText("上\n\n午");
        afternoon.setText("下\n\n午");
        right_tv.setText("课程表");
        currentDay.setText(DateTools.getCrashDay() + "\t" + getWeekOfDate(new Date()));
        String classId=BaseApplication.sUserBean.getPersonalbean().getClassId()+"";
        amAdapter = new ClasstableAdapter();
        course.setAdapter(amAdapter);
        if (classId!=null){
            unsubscribe();
        NetWork.getEndPointInterface().getCourseTab(classId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<StatusCode<List<CourseTab>>>() {
            @Override
            public void call(StatusCode<List<CourseTab>> listStatusCode) {

                if (listStatusCode.isFlag()) {
                    List<CourseTab> courseTabs = listStatusCode.getData();
                    for (int i = 0; i < courseTabs.size(); i++) {
                        setSubject(courseTabs.get(i).getSdOrder(), courseTabs.get(i).getSdWeekday(),
                                courseTabs.get(i).getMsName());
                    }
                    for (int i = 0; i < 7; i++) {
                        for (int j = 0; j < 6; j++) {
                            if (courseArr[i][j] != null) {
                                strList.add(courseArr[i][j]);
                            } else {
                                strList.add("");
                            }
                        }
                    }
                    amAdapter.notifyDataSetChanged();
                }

            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                ToastUtils.makeText(CourseTabActivity.this,"获取课程表失败");
            }
        });
    }}
    /**
     * 二维数组地位赋值
     * @param order
     * @param weekDay
     * @param subject
     */
    private void setSubject(int order, int weekDay, String subject) {

        courseArr[0][0] = "第一节";
        courseArr[1][0] = "第二节";
        courseArr[2][0] = "第三节";
        courseArr[3][0] = "第四节";
        courseArr[4][0] = "第五节";
        courseArr[5][0] = "第六节";
        courseArr[6][0] = "第七节";
        for (int i = 1; i <= 7; i++) {
            if (order == i) {
                courseArr[order - 1][weekDay] = subject;
                break;
            }
        }

    }
    /**
     * 判断当日星期几
     * @param date
     * @return
     */
    public String getWeekOfDate(Date date) {

        mWeekOfDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        Calendar calendar = Calendar.getInstance();

        if (date != null) {

            calendar.setTime(date);

        }

        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if (w < 0) {

            w = 0;
        }
        return mWeekOfDays[w];

    }
    class ClasstableAdapter extends BaseAdapter {

        public ClasstableAdapter() {
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return strList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            convertView = LayoutInflater.from(CourseTabActivity.this).inflate(R.layout.course_item, null);

            TextView subject = (TextView) convertView.findViewById(R.id.subject);
            if (strList.size() > 0 && strList.size() < 43) {
                //判断星期几
                String weekOfDate = getWeekOfDate(new Date());
                selectWeek(convertView, weekOfDate,position);

                subject.setText(strList.get(position));
            }
            return convertView;
        }

    }
    /**
     * 选中当天的课程
     * @param subject
     * @param weekOfDate
     * @param position
     */
    private void selectWeek(View subject, String weekOfDate, int position) {
        if (weekOfDate.equals(mWeekOfDays[1])) {
            //星期一
            mMImageViewOne.setVisibility(View.VISIBLE);
            if ( position%6==1){
                subject.setBackgroundResource(R.drawable.course_shape);
            }
        } else if (weekOfDate.equals(mWeekOfDays[2])) {
            //星期二
            mImageViewTwo.setVisibility(View.VISIBLE);
            if ( position%6==2){
                subject.setBackgroundResource(R.drawable.course_shape);
            }

        } else if (weekOfDate.equals(mWeekOfDays[3])) {
            //星期三
            mImageViewThreed.setVisibility(View.VISIBLE);
            if ( position%6==3){
                subject.setBackgroundResource(R.drawable.course_shape);
            }

        } else if (weekOfDate.equals(mWeekOfDays[4])) {
            mImageViewFour.setVisibility(View.VISIBLE);
            if ( position%6==4){
                subject.setBackgroundResource(R.drawable.course_shape);
            }

        } else if (weekOfDate.equals(mWeekOfDays[5])) {
            //星期五
            mImageViewFive.setVisibility(View.VISIBLE);
            if ( position%6==5){
                subject.setBackgroundResource(R.drawable.course_shape);
            }

        }


    }
}

